package com.fandou.airobot.ui.recording

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.example.alarmclockdemo.RecordHistoryAdapter
import com.fandou.airobot.R
import com.fandou.airobot.database.Record
import com.fandou.airobot.database.RecordDatabase
import com.fandou.base.view.CommonItemDecoration
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.interfaces.OnSelectListener
import kotlinx.android.synthetic.main.ai_robot_record_history_fragment.*

/**
 * @author Jm
 * @date 2020/7/9 17:05
 * @description 记录模式 历史记录
 */
class RecordHistoryFragment : Fragment(), BaseQuickAdapter.OnItemLongClickListener, BaseQuickAdapter.OnItemChildClickListener {

    private var mAdapter: RecordHistoryAdapter? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.ai_robot_record_history_fragment, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        recyclerView.layoutManager = GridLayoutManager(context, 4)
        val space = resources.getDimensionPixelSize(R.dimen.dp_16);
        recyclerView.addItemDecoration(CommonItemDecoration(space, space, space))
        mAdapter = RecordHistoryAdapter()
        recyclerView.adapter = mAdapter
        mAdapter!!.setOnItemLongClickListener(this)
        mAdapter!!.setOnItemChildClickListener(this)
        mAdapter!!.setNewData(RecordDatabase.getInstance().recordDao().allRecords)
    }

    override fun onItemLongClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int): Boolean {
        val item = adapter!!.getItem(position) as Record
//        TipConfirmDialog(requireContext()).setTitle("温馨提示").setDialogMessage("您确定要删除吗？").build().showDialog().setOnConfirmListener {
//            val count = RecordDatabase.getInstance().recordDao().deleteRecord(item)
//            if (count > 0) {
//                //delete success
//                mAdapter!!.deleteRecord(item.id)
//            }
//        }
        XPopup.Builder(context).atView(view).asAttachList(arrayOf("删除"), null, object : OnSelectListener {
            override fun onSelect(position: Int, text: String?) {
                val count = RecordDatabase.getInstance().recordDao().deleteRecord(item)
                if (count > 0) {
                    //delete success
                    mAdapter!!.deleteRecord(item.id)
                }
            }
        }).show()
        return true
    }


    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        val item = adapter!!.getItem(position) as Record
        val uri = Uri.parse(item.getVideoPath())
        val intent = Intent(Intent.ACTION_VIEW)
        intent.setDataAndType(uri, "video/*")
        context?.startActivity(intent)
    }

    companion object {
        /**
         * @param
         */
        @JvmStatic
        fun newInstance(): RecordHistoryFragment {
            return RecordHistoryFragment().also { fragment ->
                fragment.arguments = Bundle().also {
                    it.putString("params", "")
                }
            }
        }
    }

}